<template>
  <div class="form-layout">
    <!--左侧导航-->
    <div class="nav-left">
      <customAnchor
        :itemList="[
          { name: 'box-base', title: `${cpBusTitle}信息` },
          { name: 'box-file', title: `${cpBusTitle}附件` },
          { name: 'box-dtl', title: `${cpBusTitle}明细` },

        ]"
      />
    </div>
    <!--表单区域-->
    <div class="form-right" ref="formRight">
      <customForm :data="form" :rules="propRules" ref="editForm" :disabled="propReadonly">
        <template #content>
          <div id="box-base" class="box-title">{{cpBusTitle}}信息</div>
          <customFormItem
            label="操作科室"
            name="deptNameFrom"
            :parent="_self"
            :form="form"
            :readonly="true"
            :showSelectIcon="true"
            @focus="utilSelect.dept(deptNameFromChange)"
            :disabled="!propParent.cpFormNew"
          />
          <customFormItem label="盘点类型" name="checkType" :parent="_self" :form="form"
                          :type="constCommon.formItemType.selectDict.name"
                          :config="{dictType: constCommon.dictType.deviceCheckType}"
                          :disabled="!propParent.cpFormNew"
          />

          <customFormItem
            label="盘点科室"
            name="deptNameTo"
            :parent="_self"
            :form="form"
            :readonly="true"
            :showSelectIcon="true"
            @focus="utilSelect.dept(deptNameToChange)"
            :disabled="!propParent.cpFormNew"
            v-if="form.checkType == constCommon.dictType.deviceCheckTypeDept"
          />

          <customFormItem
            label="盘点供应商"
            name="supplierName"
            :parent="_self"
            :form="form"
            :readonly="true"
            :showSelectIcon="true"
            @focus="utilSelect.supplier(supplierSelect)"
            v-if="form.checkType == constCommon.dictType.deviceCheckTypeSp"
            :disabled="!propParent.cpFormNew"
          />

          <customFormItem label="单号" name="busKey" :parent="_self" :form="form" disabled/>
          <customFormItem label="操作日期" name="busDate" :parent="_self" :form="form"
                          :type="constCommon.formItemType.datePicker.name"  disabled/>
          <customFormItem label="操作人" name="busUser" :parent="_self" :form="form"  disabled/>
          <customFormItem label="操作人电话" name="applyPhone" :parent="_self" :form="form"  />
          <customFormItem label="备注" name="remark" :parent="_self" :form="form"/>

          <div id="box-file" class="box-title">{{cpBusTitle}}附件</div>
          <customFormItem
            label=""
            name="workFile"
            :parent="_self"
            :form="form"
            :type="constCommon.formItemType.slot.name"
            fullWidth
          >
            <template #content>
              <customUpload
                :data="{ fileRefId: form.id+ 'apply', fileRefTable: 'device_work_item', fileSavePath: 'device_work_item'}"
                :hide="propParent.cpFormNew"
                hideMsg="请先保存主单据后再上传"
                :max="20"
                :disabled="propReadonly"
                multiple
              />
            </template>
          </customFormItem>

          <div id="box-dtl" class="box-title">{{cpBusTitle}}明细</div>
          <customTooltip title="title" v-show="cpShowScan">
            <template #title>
              扫描设备标签码进行盘点
            </template>
            <template #content>
              <FortenScan ref="scan" propPlaceholder="请扫描设备标签码" style="width: 300px;margin-top: 10px;display: inline-block; margin-right: 5px" @after-scan="barCodeScan" propType="uniqueCode"/>
            </template>
          </customTooltip>
          <data-view ref= "childDataView"
                     :propHideFlowSelect = "true"
                     :propsDisableDbClick = "true"
                     propViewKey="device_work_item_check"
                     :propFlowKey="propParent.propFlowKey"
                     :propInitData="form"
                     :propParentPage="_self"
                     :propParentData="propParent.viewFormData"
                     :propAutoFetchData="false"
                     :propTableHide="propParent.cpFormNew"
                     propTableHideMsg="请先保存主单据"
                     class="data-view-inlinebtn"
                     @afterTableDataChange="afterTableDataChange"
                     :propIsTdesign="true"
                     :propOnlyQueryAll="propReadonly"
                     :propBeforeClick="propBeforeClickDtl"
                     :rowEditInputValid="rowEditInputValid"
          />
        </template>

      </customForm>
    </div>

    <!--采购明细选择-->
    <tableDrawer ref="tableDrawerPoDtl" refName="tableDrawerPoDtl" viewKey="device_work_item_select" attach="body"
      loadForce
       :queryWrapper="$newWrapper()"
       :propUserParam="{busType: constCommon.dictType.deviceBusTypePoWi}"
       subtitle="已完成验收明细"
    />
  </div>
</template>

<script>
import customAnchor from '@/components/custom/form/customAnchor.vue'
import customUpload from "@/components/custom/form/customUpload.vue";
import parameters from "@/util/parameters";
import constCommon from "@/util/const/constCommon";

export default {
  desc: '设备-盘点-详情',
  data() {
    return {
      form: {},
    }
  },
  computed: {
    constCommon() {
      return constCommon
    },
    // 业务类型
    cpBusType() {
      return this.propParam && this.propParam.busType ? this.propParam.busType : -1
    },
    // 业务标题
    cpBusTitle() {
      return this.utils.getDictLabel(this.cpBusType, this.constCommon.dictType.deviceBusType, null, false)
    },
    // 显示扫描框
    cpShowScan() {
      return 'submit' == this.form.flowStatus
    }
  },
  created() {
    let that = this
    this.$emit('event-register', {
      //表单校验
      validate: (callback) => {
        return this.$refs.editForm.validate(callback)
      },

      //保存前操作
      beforeSave: () => {
        this.$emit('data-callback', this.form)
        if (this.strings.isNotBlank(this.form.deptNameFrom) && this.strings.isNotBlank(this.form.deptNameTo) && this.form.deptNameFrom == this.form.deptNameTo) {
          this.utilMessage.warning('来源科室与目标科室一样，请调整')
          return false
        }
        return true
      },

      //数据加载后
      afterMove: (formData, initData) => {
        this.form = global.utils.jsonClone(formData)
        if (this.propParent.cpFormNew) {
          this.$set(this.form, 'busUser', this.utils.getCurrentUser().name)
          this.$set(this.form, 'deptIdFrom', this.utils.getCurrentUser().deptId)
          this.$set(this.form, 'deptNameFrom', this.utils.getCurrentUser().deptName)
          this.$set(this.form, 'busDate', new Date().format(this.dates.defaultDatePattern))
          this.$set(this.form, 'busType', this.cpBusType)
          this.$set(this.form, 'busKey', '自动获取')
          let hospital = this.parameters.getHospital(this, {})
          if (hospital) {
            Object.keys(hospital).forEach(key => {
              this.$set(this.form, key, hospital[key])
            })
          }
        }

        this.loadDtl()
      },

      //数据保存后
      afterSave: () => {
      },
      beforeDelete: () => {
        return true
      },

      //数据删除后
      afterDelete: () => {
      },

      //流程提交前
      beforeFlow: (flowButtonKey) => {
        return true
      },

      //流程提交后
      afterFlow: (flowButtonKey) => {
        that.loadDtl()
      },

      //表单关闭前
      beforeClose: () => {
        return true
      }
    })
  },
  props: {
    //上级对象
    propParent: Object,
    //上级初始化数据
    propInitData: Object,
    //主键字段
    propPrimaryKey: String,
    //数据字典
    propDictionaries: Object,
    //当前操作行
    propCurrentRow: Object,
    //表单数据
    propViewFormData: Object,
    //是否只读
    propReadonly: Boolean,
    //操作状态
    propStatus: String,
    //表单规则
    propRules: Object,
    propParam: Object,
  },
  components: {
    customUpload,
    customAnchor
  },
  methods: {

    // 加载明细
    loadDtl() {
      let queryWrapper = this.$newWrapper()
      if (this.form.id) {
        queryWrapper.eq('workId', this.form.id)
      } else {
        queryWrapper.eq('workId', -1)
      }
      this.$refs.childDataView.setQueryWrapper(queryWrapper)
    },

    // 明细 新增、删除、修改后调用
    afterTableDataChange() {
      this.utils.refreshRefs(this)
    },
    // 来源科室选择
    deptNameFromChange(item) {
      this.$set(this.form, 'deptIdFrom', item.id)
      this.$set(this.form, 'deptNameFrom', item.name)
    },
    // 盘点科室选择
    deptNameToChange(item) {
      this.$set(this.form, 'deptIdTo', item.id)
      this.$set(this.form, 'deptNameTo', item.name)
    },
    // 选择完采购明细
    tableDrawerPoDtlConfirm(rows) {
      //转换属性
      rows.forEach(ob => {
        ob.workId = this.form.id
        ob.workItemId = ob.id
        ob.deviceQty = ob.deviceQtyAccept
        ob.id = null
      })

      //批量添加
      this.utilVue.loadShow(true)
      this.$ajax.post('/device/work_item/saveBatch', rows).then(resp => {
        this.utilVue.loadHide()
        if (resp.code == 0) {
          this.utilMessage.success(resp.msg)
          this.loadDtl()
        }
      })
    },
    // 选择完采购明细
    propBeforeClickDtl(button, formData , rowData, that) {
      if (button.key == 'vb-add') {
        this.$refs.tableDrawerPoDtl.show(this.tableDrawerPoDtlConfirm, true)
        return false
      }
      return true
    },
    // 单元格输入校验
    rowEditInputValid(row, column,field, value) {
      if (field == 'deviceQtyAccept' && value > row.deviceQty) {
        this.utilMessage.warning('验收数量不能大于采购数量')
        return false
      }
      return true
    },

    //供应商选择
    supplierSelect(row, refName) {
      this.$set(this.form, 'supplierId', row.id)
      this.$set(this.form, 'supplierName', row.name)
    },

    //扫码SN或RFID
    barCodeScan(txtScan) {
      this.utilVue.loadShow(true)
      this.$ajax.post('/device/work_item/addByScanForCheck', {'workId': this.form.id, 'deviceSn': txtScan}).then(
        resp => {
          if (resp.code == 0) {
            this.utilMessage.success(resp.msg)
            this.loadDtl()
          }
          this.utilVue.loadHide()
        }
      )
    }
  }
}
</script>
